package com.jeff.android.lab.module.anim.path;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.jeff.android.lab.R;

/**
 * @author: duff
 * @date: 2019/1/2
 * @since: 1.0.0
 */
public class AnimTestFragment extends Fragment {

    ImageView bg_image;

    WaveView mWaveView;

    float percent = .0f;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mWaveView.setPercent(percent);
            percent += 0.02f;
            if (percent <= 1.0f)
                mHandler.sendEmptyMessageDelayed(0, 500);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anim_test,
                container, false);
        mWaveView = view.findViewById(R.id.wave_view);

        bg_image = view.findViewById(R.id.bg_image);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)bg_image.getLayoutParams();
        lp.rightMargin = 50;

        return view;
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button btn1 = view.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWaveView.start();
                mHandler.sendEmptyMessageDelayed(0, 500);
            }
        });

        Button btn2 = view.findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWaveView.end();
            }
        });
    }
}
